package org.mybatis.generator.plugins;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;


public class LombokPlugin extends PluginAdapter {

    private static final List<String> PARENT_PROPERTIES = Stream.of("createTime", "createBy", "updateTime", "updateBy", "isDeleted", "version", "remark").collect(Collectors.toList());


    @Override
    public boolean validate(List<String> list) {
        return true;
    }

    /**
     * 添加注解的方法
     */
    @Override
    public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {

        // 添加实体类的 import
        topLevelClass.addImportedType("lombok.Data");
        topLevelClass.addImportedType("lombok.NoArgsConstructor");
        topLevelClass.addImportedType("lombok.AllArgsConstructor");
//        topLevelClass.addImportedType("lombok.experimental.Accessors");
        topLevelClass.addImportedType("lombok.Builder");
        topLevelClass.addImportedType("lombok.ToString");
        // 添加实体类的 lombok 注解
        topLevelClass.addAnnotation("@Data");
//        topLevelClass.addAnnotation("@Accessors(chain = true)");
        topLevelClass.addAnnotation("@NoArgsConstructor");
        topLevelClass.addAnnotation("@AllArgsConstructor");
        topLevelClass.addAnnotation("@Builder");
        topLevelClass.addAnnotation("@ToString");

        return true;
    }

    @Override
    public boolean modelSetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        // 不生成 setter
        return false;
    }

    @Override
    public boolean modelGetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        // 不生成 getter
        return false;
    }

    @Override
    public boolean modelFieldGenerated(Field field, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        // 如果字段名存在于父类属性中，返回 false 以排除生成
        if (PARENT_PROPERTIES.contains(field.getName())) {
            return false;
        }
        return true;
//        return super.modelFieldGenerated(field, topLevelClass, introspectedColumn, introspectedTable, modelClassType);
    }

    /* @Override
    public boolean sqlMapResultMapWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
        // 添加父类字段到 ResultMap 中
        addParentFieldsToResultMap(element);
        return super.sqlMapResultMapWithoutBLOBsElementGenerated(element, introspectedTable);
    }

    @Override
    public boolean sqlMapBaseColumnListElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
        // 添加父类字段到 baseColumnList 中
        addParentFieldsToBaseColumnList(element);
        return super.sqlMapBaseColumnListElementGenerated(element, introspectedTable);
    }*/

    private void addParentFieldsToResultMap(XmlElement element) {
        // 定义父类字段
        String[] parentFields = {"create_time", "create_by", "update_time","update_by","is_deleted","version","remark"};

        for (String field : parentFields) {
            XmlElement result = new XmlElement("result");
            result.addAttribute(new Attribute("column", field));
            result.addAttribute(new Attribute("property", convertColumnToProperty(field)));
            result.addAttribute(new Attribute("jdbcType", "VARCHAR")); // 需要根据具体类型修改
            element.addElement(result);
        }
    }

    private void addParentFieldsToBaseColumnList(XmlElement element) {
        // 定义父类字段
        String[] parentFields = {"create_time", "create_by", "update_time","update_by","is_deleted","version","remark"};

        StringBuilder sb = new StringBuilder();
        sb.append(",");
        for (String field : parentFields) {
            sb.append(field).append(", ");
        }

        // 删除最后一个逗号和空格
        String parentColumns = sb.substring(0, sb.length() - 2);
        element.addElement(new TextElement(parentColumns));
    }

    private String convertColumnToProperty(String column) {
        // 将下划线命名的字段名转换为驼峰命名的属性名
        String[] parts = column.split("_");
        StringBuilder property = new StringBuilder(parts[0]);
        for (int i = 1; i < parts.length; i++) {
            property.append(parts[i].substring(0, 1).toUpperCase()).append(parts[i].substring(1).toLowerCase());
        }
        return property.toString();
    }
}
